﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using XCode;
using XCode.Configuration;

namespace BingZi.HY.Core.Entities.Center
{
	/// <summary>
	/// 店铺分类信息
	/// </summary>
	public partial class app_ShopCategory : HYEntityBase<app_ShopCategory>
	{
		#region 对象操作
		//基类Entity中包含三个对象操作：Insert、Update、Delete
		//你可以重载它们，以改变它们的行为
		//如：
		/*
		/// <summary>
		/// 已重载。把该对象插入到数据库。这里可以做数据插入前的检查
		/// </summary>
		/// <returns>影响的行数</returns>
		public override Int32 Insert()
		{
			return base.Insert();
		}
		 * */
		#endregion
		
		#region 扩展属性
		//TODO: 本类与哪些类有关联，可以在这里放置一个属性，使用延迟加载的方式获取关联对象

		/*
		private Category _Category;
		/// <summary>该商品所对应的类别</summary>
		public Category Category
		{
			get
			{
				if (_Category == null && CategoryID > 0 && !Dirtys.ContainKey("Category"))
				{
					_Category = Category.FindByKey(CategoryID);
					Dirtys.Add("Category", true);
				}
				return _Category;
			}
			set { _Category = value; }
		}
		 * */
		#endregion

		#region 扩展查询
		/// <summary>
		/// 根据主键查询一个店铺分类信息实体对象用于表单编辑
		/// </summary>
		///<param name="__CategoryID">分类编号</param>
		/// <returns></returns>
		[DataObjectMethod(DataObjectMethodType.Select, false)]
		public static app_ShopCategory FindByKeyForEdit(Int32 __CategoryID)
		{
			app_ShopCategory entity=Find(new String[]{_.CategoryID}, new Object[]{__CategoryID});
			if (entity == null)
			{
				entity = new app_ShopCategory();
			}
			return entity;
		}     
		#endregion

		#region 高级查询
		/// <summary>
		/// 查询满足条件的记录集，分页、排序
		/// </summary>
		/// <param name="key">关键字</param>
		/// <param name="orderClause">排序，不带Order By</param>
		/// <param name="startRowIndex">开始行，0表示第一行</param>
		/// <param name="maximumRows">最大返回行数，0表示所有行</param>
		/// <returns>实体集</returns>
		[DataObjectMethod(DataObjectMethodType.Select, true)]
		public static EntityList<app_ShopCategory> Search(String key, String orderClause, Int32 startRowIndex, Int32 maximumRows)
		{
		    return FindAll(SearchWhere(key), orderClause, null, startRowIndex, maximumRows);
		}

		/// <summary>
		/// 查询满足条件的记录总数，分页和排序无效，带参数是因为ObjectDataSource要求它跟Search统一
		/// </summary>
		/// <param name="key">关键字</param>
		/// <param name="orderClause">排序，不带Order By</param>
		/// <param name="startRowIndex">开始行，0表示第一行</param>
		/// <param name="maximumRows">最大返回行数，0表示所有行</param>
		/// <returns>记录数</returns>
		public static Int32 SearchCount(String key, String orderClause, Int32 startRowIndex, Int32 maximumRows)
		{
		    return FindCount(SearchWhere(key), null, null, 0, 0);
		}

		/// <summary>
		/// 构造搜索条件
		/// </summary>
		/// <param name="key">关键字</param>
		/// <returns></returns>
		private static String SearchWhere(String key)
		{
            if (String.IsNullOrEmpty(key)) return null;
            key = key.Replace("'", "''");
            String[] keys = key.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

		    StringBuilder sb = new StringBuilder();

            for (int i = 0; i < keys.Length; i++)
            {
                if (sb.Length > 0) sb.Append(" And ");

                if (keys.Length > 1) sb.Append("(");
                Int32 n = 0;
                foreach (FieldItem item in Meta.Fields)
                {
                    if (item.Property.PropertyType != typeof(String)) continue;
                    // 只要前五项
                    if (++n > 5) break;

                    if (n > 1) sb.Append(" Or ");
                    sb.AppendFormat("{0} like '%{1}%'", Meta.FormatKeyWord(item.Name), keys[i]);
                }
                if (keys.Length > 1) sb.Append(")");
            }

            return sb.ToString();
		}


        /// <summary>
        /// 查询满足条件的记录集，分页、排序
        /// </summary>
        /// <param name="qp">查询构造器</param>
        /// <param name="RecordCount">最大结果数量</param>
        /// <returns>实体集</returns>
        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public static EntityList<app_ShopCategory> FindAll(BingZi.HY.Core.BLL.QueryParam qp, out int RecordCount)
        {
            //构造排序字段
            string orderClause = string.Empty;
            if (qp.Orderfld == string.Empty)
            {
                foreach (FieldItem item in Meta.Fields)
                {
                    if (item.IsIdentity)
                    {
                        qp.Orderfld = item.Name;
                        break;
                    }
                }
            }
            orderClause = string.Format("{0} {1}", qp.Orderfld, qp.OrderType == 1 ? "desc" : "asc");

            //构造查询条件
            StringBuilder sb = new StringBuilder();
            foreach (BingZi.HY.Core.BLL.SearchParam sp in qp.Where)
            {
                if (sb.Length > 0) sb.Append(" And ");

                sb.Append(sp.ToSql());
            }
            //返回当前最大结果数量
            qp.RecordCount = RecordCount = FindCount(sb.ToString(), null, null, 0, 0);
            //返回所有结果集
            return FindAll(sb.ToString(), orderClause,qp.ReturnFields, qp.startRowIndex, qp.PageSize);
        }
		#endregion

		#region 扩展操作
		#endregion

		#region 业务
		#endregion
	}
}